//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

import delimited using "data\pollsclose.csv", clear varn(1)
save "data\scratch\pollsclose", replace


import delimited using "data\nyt_counties.csv", clear varn(1)
replace state = lower(state)
replace state = subinstr(state," ","-",.)
merge m:1 state using "data\scratch\pollsclose",nogen


*calculate time from closing

gen time_num = clock(time,"YMD hms")
gen hours_from_close = time_num - clock("2020-11-04 01:00:00","YMD hms")
replace hours_from_close = hours_from_close/(60*60*1000)
drop if hours_from_close < 0

gen final_votes = votes if name ~= name[_n+1]
egen max_final_votes = max(final_votes),by(name)
drop final_votes
rename max_final_votes final_votes
gen pct_reported = 100*votes/final_votes



** calculate which candidate won each county

egen max_bidenj = max(bidenj) if name ~= "STATE",by(name)
egen max_trumpd = max(trumpd) if name ~= "STATE",by(name)
gen biden_win = max_bidenj > max_trumpd

* create separate dataset for the whole state
preserve
sort hours_from_close
drop if totalvotes == totalvotes[_n-1]
keep hours_from_close totalvotes
gen name = "STATE"
egen final_votes = max(totalvotes)
gen pct_reported = 100*totalvotes/final_votes
save "data\scratch\state", replace
restore



preserve
sort name hours_from_close

gen pct_reported2 = pct_reported if hours_from_close < 2 & hours_from_close[_n+1] > 2
gen pct_reported4 = pct_reported if hours_from_close < 4 & hours_from_close[_n+1] > 4
gen pct_reported8 = pct_reported if hours_from_close < 8 & hours_from_close[_n+1] > 8
gen pct_reported16 = pct_reported if hours_from_close < 16 & hours_from_close[_n+1] > 16
gen pct_reported24 = pct_reported if hours_from_close < 24 & hours_from_close[_n+1] > 24

gen bidenpct = 100*max_bidenj/(max_bidenj + max_trumpd)

collapse (max) pct_reported* final biden_win bidenpct,by(name)
recode pct_reported* (100/max = 100) (. = 0)

corr pct_reported* final
corr pct_reported* bidenpct
restore

*show first 8 hours for Georgia

keep if state == "georgia"
keep if hours_from_close <= 8

//save version of data here 
save "data\scratch\temp16data", replace
use "data\scratch\temp16data", clear

preserve
collapse (count) votes, by(name)
drop votes
gen hours_from_close = 0
gen votes = 0
gen pct_reported = 0
save "data\scratch\hours_from_close0", replace
drop pct_reported
replace hours_from_close = 8
save "data\scratch\hours_from_close8", replace
replace hours_from_close = 8.1
save "data\scratch\hours_from_close8_1", replace

restore

append using "data\scratch\hours_from_close0"
append using "data\scratch\hours_from_close8"
append using "data\scratch\hours_from_close8_1"
append using "data\scratch\state"
sort name hours_from_close
replace votes = votes[_n-1] if hours_from_close == 8
replace pct_reported = pct_reported[_n-1] if hours_from_close == 8
replace final_votes = final_votes[_n+1] if hours_from_close == 0
replace final_votes = final_votes[_n-1] if hours_from_close == 8
drop if pct_reported == pct_reported[_n-1] & name == name[_n-1] & hours_from_close ~= 8

** create two additional observations for the state
set obs `=_N + 2'
replace name = "STATE" in -2/l
replace hours_from_close = 0 in -2
replace pct_reported = 0 in -2
replace hours_from_close = 8 in l
sort name hours_from_close
replace pct_reported = pct_reported[_n-1] if hours_from_close == 8 & name == "STATE"
drop if name == "STATE" & name[_n-1] == "STATE" & pct_reported < pct_reported[_n-1]

replace biden_win = biden_win[_n-1] if name == name[_n-1] & biden_win == .
replace biden_win = biden_win[_n+1] if name == name[_n+1] & biden_win == .


twoway (connected pct_reported hours_from_close if name ~= "STATE" & biden_win == 1 [aweight = final_votes], ///
          mcolor(midblue%60) msize(tiny) msymbol(circle_hollow) lcolor(gray%60) lwidth(thin) connect(stairstep) cmissing(n)) ///
       (connected pct_reported hours_from_close if biden_win == 0 [aweight = final_votes], ///
	      mcolor(red%60) msize(tiny) msymbol(circle_hollow) lcolor(gray%60) lwidth(thin) ///
		  lpattern(solid) connect(stairstep) cmissing(n)), ///
	   ytitle(Percent) ///
	   xtitle(Hours from Close) ///
	   legend(order(1 "Countes Biden won" 2 "Counties Trump won" 3 "Statewide") size(small) span)
	   graph export "figures\fig16.png", replace width(2500)	   
